/**
 * 
 */
package edu.umd.clip.lm.model.decoding;

import java.util.Arrays;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import edu.umd.clip.lm.model.decoding.VariationalTagger.VariationalPath;
import edu.umd.clip.lm.model.decoding.TrellisSlice.SliceItem;
import edu.umd.clip.lm.util.BinaryPrefix;

/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class VariationalTaggerContext {
	static class Context {
		final int ctx[];
	
		public Context(int[] ctx) {
			this.ctx = ctx;
		}
	
		@Override
		public int hashCode() {
			return Arrays.hashCode(ctx);
		}
	
		@Override
		public boolean equals(Object obj) {
			Context other = (Context) obj;
			return Arrays.equals(ctx, other.ctx);
		}
	}

	protected Map<SliceItem, Integer> itemTagHash;
	private BinaryPrefix tags[];
	TreeSet<VariationalPath> currentPaths;
	HashMap<Context,List<VariationalPath>> currentPathsHash;

	VariationalTaggerContext(BinaryPrefix tags[]) {
		this.tags = tags;
		itemTagHash = new IdentityHashMap<SliceItem, Integer>();	
		currentPathsHash = new HashMap<VariationalTaggerContext.Context,List<VariationalPath>>();
		currentPaths = new TreeSet<VariationalPath>();
	}

	protected int getItemTag(SliceItem item) {
		Integer itemTag = itemTagHash.get(item);
		if (itemTag == null) {
			for(int tag=0; tag < tags.length; ++tag) {
				BinaryPrefix tagPrefix = tags[tag];
				if (tagPrefix == null) continue;
				if (tagPrefix.isPrefixOf(item.tree.getPrefix())) {
					itemTag = tag;
					itemTagHash.put(item, itemTag);
					return tag;
				}
			}
			//System.out.printf("item %s is not a tag: treeProb=%g\n", item, item.tree.getProbTree().getTreeProb());
			return -1;
		}
		return itemTag.intValue();
	}
	

}
